home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-06-22 | 5.8 KB | 256 lines | [TEXT/CWIE] |
- //other procs
- #include "MemoryUtil.h"
-
- #include "otherprocs.h"
-
-
- OSErr
- SaveJPEGFromQuickCam ( FSSpec theFileSpec, short digitizerDepth )
- {
- OSErr theErr;
- PicHandle thePict;
- ComponentDescription theQuickCamDesc;
- Component theQuickCam;
- ComponentInstance theQuickCamInstance;
- Rect maxSrcRect;
- CGrafPtr theWorld;
- CTabHandle cTable;
- PixMapHandle thePMH;
- short videoInputSource;
- GWorldPtr origGWorld;
- GDHandle origGDevice;
- long imageDataSize;
- SInt16 theRefNum;
-
- long maxCompressedSize;
- ImageDescriptionHandle compressedImageDesc;
- Ptr compressedImageData;
-
- thePict = (PicHandle)NewHandle( 0 );
- if ( !thePict || MemError( ) )
- return nil;
-
- //look for a color QuickCam
- theQuickCamDesc.componentType = sgVideoDigitizerType;
- theQuickCamDesc.componentSubType = 'CQCm';
- theQuickCamDesc.componentManufacturer = 'Ctx7';
- theQuickCamDesc.componentFlags = 0;
- theQuickCamDesc.componentFlagsMask = 0;
-
- theQuickCam = FindNextComponent( 0, &theQuickCamDesc );
- if ( !theQuickCam ) //look for the B&W
- {
- theQuickCamDesc.componentType = sgVideoDigitizerType;
- theQuickCamDesc.componentSubType = 'CtxV';
- theQuickCamDesc.componentManufacturer = 'Ctx6';
- theQuickCamDesc.componentFlags = 0;
- theQuickCamDesc.componentFlagsMask = 0;
- }
- if ( !theQuickCam )
- return nil;
-
- theQuickCamInstance = OpenComponent( theQuickCam );
- if ( !theQuickCamInstance )
- return nil;
-
- theErr = VDGetInput( theQuickCamInstance, &videoInputSource );
- if ( theErr )
- return nil;
-
- theErr = VDGetMaxSrcRect( theQuickCamInstance, videoInputSource, &maxSrcRect );
- if ( theErr )
- return nil;
-
- theErr = VDSetDigitizerRect( theQuickCamInstance, &maxSrcRect );
- if ( theErr )
- return nil;
-
- cTable = digitizerDepth != 32 && digitizerDepth != 16 ? GetCTable( digitizerDepth ) : nil;
- theErr = NewGWorld( &theWorld, digitizerDepth, &maxSrcRect, cTable, nil, keepLocal );
- if ( cTable )
- DisposeCTable( cTable );
- if ( theErr )
- return nil;
-
- thePMH = GetGWorldPixMap( theWorld );
- LockPixels( thePMH );
-
- theErr = VDSetPlayThruDestination( theQuickCamInstance, thePMH, &maxSrcRect, nil, nil );
- if ( theErr )
- return nil;
-
- theErr = VDGrabOneFrame( theQuickCamInstance );
- if ( theErr )
- return nil;
-
- GetGWorld( &origGWorld, &origGDevice );
- SetGWorld( theWorld, nil); //get the pictures to save at the proper coordinates
-
- // thePict = OpenPicture( &maxSrcRect );
- // HLock( (Handle)thePMH );
- // CopyBits( (BitMapPtr)*thePMH, (BitMapPtr)*thePMH, &maxSrcRect, &maxSrcRect, srcCopy, nil );
- // HUnlock( (Handle)thePMH );
- // ClosePicture( );
-
- //•••••••••••••••••••••••••••••••••••••
-
- theErr = GetMaxCompressionSize ( thePMH, &maxSrcRect, 32, codecMinQuality, 'jpeg', anyCodec, &maxCompressedSize );
- if ( theErr == noErr )
- {
- compressedImageData = MemoryNewPtr ( maxCompressedSize, &theErr );
- if ( compressedImageData != NULL )
- {
- compressedImageDesc = (ImageDescriptionHandle) MemoryNewHandle ( sizeof(ImageDescription), &theErr );
- if ( compressedImageDesc != NULL )
- {
- theErr = CompressImage ( thePMH, &maxSrcRect, codecMinQuality, 'jpeg', compressedImageDesc, compressedImageData );
-
- if ( theErr == noErr )
- {
- theErr = FSpDelete ( &theFileSpec );
- theErr = FSpCreate ( &theFileSpec, 'JVWR', 'JPEG', smSystemScript );
- }
-
- if ( theErr == noErr )
- {
- theErr = FSpOpenDF ( &theFileSpec, fsWrPerm, &theRefNum );
- if ( theErr == noErr )
- {
- imageDataSize = (*compressedImageDesc)->dataSize;
- theErr = FSWrite ( theRefNum, &imageDataSize, compressedImageData );
-
- FSClose ( theRefNum );
- }
- }
- }
- }
- }
-
- //clean up
- DisposeHandle ( (Handle)compressedImageDesc );
- DisposePtr ( compressedImageData );
-
-
- //•••••••••••
- if ( theWorld )
- DisposeGWorld( theWorld );
-
- SetGWorld( origGWorld, origGDevice );
-
- return theErr;
- }
-
-
-
-
- #if 0
-
- /* this is modded from whatson */
-
- void
- SavePictAsJPEG ( PicHandle thePict )
- {
- LayerPtr rootLayer;
- WindowPtr frontmostWindow;
- Rect frontmostRect;
- short theRefNum;
- short vRefNum;
- Str255 fileName;
- long startTime;
- OSErr err;
- Ptr compressedResultPtr;
- PixMapHandle frontmostPixMap;
- ImageDescriptionHandle imageDescriptionHandle;
-
- startTime = TickCount();
-
- /* 4000000 is a totally arbitrary 'very big' value */
- compressedResultPtr = NewPtr ( 4000000 );
- err = MemError ();
- if ( err != noErr )
- {
- *width = nil;
- *height = nil;
- return;
- }
-
- imageDescriptionHandle = (ImageDescriptionHandle) NewHandle ( sizeof(ImageDescription) );
- err = MemError ();
- if ( err != noErr )
- {
- *width = nil;
- *height = nil;
- return;
- }
-
- err = CompressImage ( frontmostPixMap, &frontmostRect, codecNormalQuality, 'jpeg', imageDescriptionHandle, compressedResultPtr );
- if ( err != noErr )
- {
- *width = nil;
- *height = nil;
- return;
- }
-
- // SetPort(thePort);
-
- /* "they're running out of ideas, for sure"
- -- Grant Neufeld, on Hollywood, for opening logo sequences */
-
- //Create a file and stuff it full of the jpeg data
-
- GetVol ( fileName, &vRefNum );
- strcpy ( (char*)fileName, (char*)"\pfrontwindow.jpg" );
-
- err = FSDelete ( fileName, vRefNum );
- if ( err != noErr )
- {
- *width = nil;
- *height = nil;
- return;
- }
-
- err = Create ( fileName, vRefNum, 'JVWR', 'JPEG' );
- if ( err != noErr )
- {
- *width = nil;
- *height = nil;
- return;
- }
-
- err = FSOpen ( fileName, vRefNum, &theRefNum );
- if ( err != noErr )
- {
- *width = nil;
- *height = nil;
- return;
- }
-
- {
- long count;
-
- count = (*imageDescriptionHandle)->dataSize;
- err = FSWrite ( theRefNum, &count, compressedResultPtr );
- if ( err != noErr )
- {
- *width = nil;
- *height = nil;
- return;
- }
- }
- err = FSClose ( theRefNum );
- if ( err != noErr )
- {
- *width = nil;
- *height = nil;
- return;
- }
-
- //clean up
- DisposeHandle ( (Handle)imageDescriptionHandle );
- DisposePtr ( compressedResultPtr );
- }
-
- #endif /* 0 */
-
-
-